#include<stdio.h>
#include<stdlib.h>
#include<string.h>


char* map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char map_find(char in)
{
	char i;
	for(i=0;i<64;i++) 
		if(map[i]==in) return i;
	return 0;
}
void convert_by_group(char* source,char* result)
{	
	int a=map_find(source[0]);
	int b=map_find(source[1]);
	int c=map_find(source[2]);
	int d=map_find(source[3]);
	result[0]=(a<<2)|(b>>4);
	result[1]=(b<<4)|(c>>2);
	result[2]=(c<<6)|d;
}
void convert_base64_to_char(char* source,char* dest,int max)
{	
	int i=0;
	int count=0;
	char tmp[8];
	char* buffer=malloc(sizeof(char)*(max+4));
	memset(buffer,0,sizeof(buffer));
	memset(dest,0,sizeof(dest));
	memcpy(buffer,source,sizeof(char)*max);
	while((buffer[i]!=0||i%4!=0)&&i<max)
	{
		if((i+1)%4==0)
		{
			memset(tmp,0,sizeof(tmp));
			memcpy(tmp,buffer+i-3,4);
			convert_by_group(tmp,dest+count);
			count+=3;
		}
		i++;
	}
}


int convert_thunder_to_url(char* source,char* dest,int max)
{
	int i;
	char head[12];
	int len;
	char* tmp_dest=malloc(sizeof(char)*(max+8));
	memset(head,0,sizeof(head));
	memset(dest,0,sizeof(dest));
	memset(tmp_dest,0,sizeof(tmp_dest));
	memcpy(head,source,sizeof(char)*10);
	head[11]=0;
	if(strcmp(head,"thunder://")==0)
	{
		convert_base64_to_char(source+10,tmp_dest,max);
		len=strlen(tmp_dest);
		if(tmp_dest[0]=='A'&&tmp_dest[1]=='A'&&tmp_dest[len-2]=='Z'&&tmp_dest[len-2]=='Z')
		{
			tmp_dest[len-2]=0;
			strcpy(dest,tmp_dest+2);
		}
		else return 0;
		return 1;
	}
	else return 0;

}
